package com.samshow.business.service;

import com.samshow.base.constant.Constant;
import com.samshow.base.program.Program;
import com.samshow.base.program.TextProgram;
import com.samshow.base.program.TextProgramFactory;
import com.samshow.base.program.TextProgramFactory_;
import com.samshow.base.util.DateUtil;
import com.samshow.baseVo.CaptionRequest;
import com.samshow.baseVo.DeleteCaptionRequest;
import com.samshow.core.cache.ScreenCache;
import com.samshow.res.entity.ScreenEntity;
import com.samshow.res.entity.TEquipProgramEntity;
import com.samshow.res.entity.TProgramEntity;
import com.samshow.res.repository.ScreenRepository;
import com.samshow.res.repository.TEquipProgramRepository;
import com.samshow.res.repository.TProgramRepository;
import com.samshow.res.vo.DefaultBxTextProgram;
import onbon.bx05.Bx5GScreen;
import onbon.bx05.file.BxFileWriterListener;
import onbon.bx05.file.ProgramBxFile;
import onbon.bx05.message.common.ErrorType;
import onbon.bx05.message.global.ACK;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by niwb on 2017/2/21.
 */
@Service
public class CaptionService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ScreenRepository screenRepository;
    @Autowired
    private TProgramRepository tProgramRepository;
    @Autowired
    private TEquipProgramRepository tEquipProgramRepository;
    @Autowired
    private BxFileWriterListener bxFileWriterListener;
    @Autowired
    private DefaultBxTextProgram defaultBxTextProgram;


    public void sendCaption(CaptionRequest captionRequest) {
        String netIds = captionRequest.getNetIds();
        String[] netIdArray = netIds.split(",");
        for (String netId : netIdArray) {
            ScreenEntity screen = screenRepository.findById(netId);
            Bx5GScreen bxScreen = ScreenCache.INSTANCE.getScreen(netId);
            if (screen != null && bxScreen != null) {
                Program program = new TextProgram(new TextProgramFactory(screen, captionRequest, defaultBxTextProgram));
                ProgramBxFile pbf = (ProgramBxFile) program.prepareProgram();
                TProgramEntity tProgramEntity = new TProgramEntity();
                tProgramEntity.setcContent(captionRequest.getCaptions());
                tProgramEntity.setdCreate(DateUtil.getCurrentTimestamp());
                tProgramEntity.setnHeight(screen.getHeight());
                tProgramEntity.setnType(Constant.ScreenType.SCREEN_TYPE_BX);
                //tProgramEntity.s
                bxScreen.writeProgramAsync(pbf, bxFileWriterListener);
            }
        }

    }

    /**
     * 删除节目
     * @param deleteCaptionRequests
     */
    public void deleteProgram(List<DeleteCaptionRequest> deleteCaptionRequests) {
        for (DeleteCaptionRequest deleteCaptionRequest : deleteCaptionRequests) {
            String netId = deleteCaptionRequest.getNetId();//屏幕编号
            Bx5GScreen bxScreen = ScreenCache.INSTANCE.getScreen(netId);
            if (bxScreen != null) {
                Bx5GScreen.Result<ACK> result = bxScreen.deletePrograms(deleteCaptionRequest.getProgramName());
                if (result.isOK()) {
                    ACK ack = result.reply;
                    //删除节目设备关系
                    tEquipProgramRepository.deletByEidAndName(netId,deleteCaptionRequest.getProgramName());
                } else {
                    ErrorType error = result.getError();
                }
            }
        }
    }

    /**
     * 发送节目单屏
     * @param netId 设备ID
     * @param pId 节目ID
     */
    public void sendCaptionSingle(String netId, String pId) {
        TProgramEntity tProgramEntity = tProgramRepository.findById(pId);
        if (tProgramEntity == null) {
            //TODO return code
        }
        ScreenEntity screen = screenRepository.findById(netId);
        if (screen == null) {
            //TODO return code
        }
        Bx5GScreen bxScreen = ScreenCache.INSTANCE.getScreen(netId);
        if (screen != null && bxScreen != null) {
            Program program = new TextProgram(new TextProgramFactory_(screen, tProgramEntity, defaultBxTextProgram));
            ProgramBxFile pbf = (ProgramBxFile) program.prepareProgram();
            bxScreen.writeProgramAsync(pbf, bxFileWriterListener);
            //建立节目设备关系
            TEquipProgramEntity tEquipProgramEntity = new TEquipProgramEntity();
            tEquipProgramEntity.setcEquipId(netId);
            tEquipProgramEntity.setnPId(Integer.parseInt(pId));
            tEquipProgramEntity.setcName(pbf.getFileName());
            tEquipProgramRepository.save(tEquipProgramEntity);
        }
    }

    /**
     * 添加节目
     * @param userId 用户ID
     * @param programName 节目名称
     * @param content 节目内容
     */
    public boolean addProgram(String userId, String programName, String content) {
        try {
            TProgramEntity tProgramEntity = new TProgramEntity();
            tProgramEntity.setcName(programName);
            tProgramEntity.setcContent(content);
            tProgramEntity.setnUserId(Integer.valueOf(userId));
            tProgramRepository.save(tProgramEntity);
            return true;
        } catch (Exception e) {
            log.error("addProgram ERROR",e);
            return false;
        }
    }
}
